//
//  HotPostViewController.m
//  HeSaid
//
//  Created by MWJ on 15/5/4.
//  Copyright (c) 2015年 MWJ. All rights reserved.
//

#import "HotPostViewController.h"
#import "HotPostCell.h"
#import "PostDetailViewController.h"
#import "hotPost.h"
#import "AppDelegate.h"

#define hotPostURL @"http://app.jiwaipro.com:8888/hesaid/index.php/HeSaid/hotPost"

@interface HotPostViewController (){
    NSMutableArray *hotPostArray;
    NSMutableArray *array;
    AppDelegate *app;
}

@end

@implementation HotPostViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title=@"热门帖";
    app=(AppDelegate *)[[UIApplication sharedApplication]delegate];
    hotPostArray=[NSMutableArray array];
    array=[NSMutableArray array];
    
    //请求帖子详情数据
        NSString *poststr =@"";
    [self londingPath:hotPostURL Str:poststr];

    
}
#pragma maek - 返回cell的行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return array.count;
}

#pragma maek - 返回cell的高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60;
}

#pragma maek - 加载cell的内容
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UINib *nib=[UINib nibWithNibName:@"HotPostCell" bundle:nil];
    [self.hotPostTableView registerNib:nib forCellReuseIdentifier:@"hotPost"];
    
    static NSString *cellIdentifier=@"hotPost";
    HotPostCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    hotPost *hotP=array[indexPath.row];
    
    cell.postReleaseTime.text= [NSString stringWithFormat:@"%@",hotP.releaseTime];
    cell.commentsNumber.text=[NSString stringWithFormat:@"%@",hotP.postComment];
    cell.postContent.text=[NSString stringWithFormat:@"%@",hotP.postContent];
    return cell;
}

#pragma maek - cell的点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    hotPost *hotP=array[indexPath.row];
    app.message_id = hotP.messageId;
    PostDetailViewController *postDetail=[self.storyboard instantiateViewControllerWithIdentifier:@"postDetailVC"];
    [self.navigationController pushViewController:postDetail animated:YES];
    
}

#pragma maek - 请求帖子详情数据
-(void)londingPath:(NSString *)path Str:(NSString *)string{
    NSURL *url = [NSURL URLWithString:path];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"POST"];
    NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
    [request setHTTPBody:data];
    [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        hotPostArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
        [self performSelectorOnMainThread:@selector(refreshData:) withObject:hotPostArray waitUntilDone:YES];
    }];
}
-(void)refreshData:(NSMutableArray *)sender{
    for (NSDictionary *dic in sender) {
        hotPost *hotpost=[[hotPost alloc]init];
        hotpost.releaseTime=dic[@"message_time"];
        hotpost.postContent=dic[@"message_content"];
        hotpost.postComment=dic[@"message_Comment"];
        hotpost.messageId = dic[@"message_id"];
        app.message_id=dic[@"message_id"];
        [array addObject:hotpost];
    }
    [self.hotPostTableView reloadData];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
